<template>
  <div>
    <el-form :inline="true" :model="from" class="demo-form-inline">
      <el-form-item label="角色姓名">
        <el-input v-model="from.RoleName" placeholder="角色姓名"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getrolelist">查询</el-button>
        <el-button type="primary" @click="dialogFormVisible = true"
          >添加角色</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      ref="multipleTable"
      :data="RoleData"
      tooltip-effect="dark"
      style="width: 100%; margin: 0, auto"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column prop="roleName" label="角色名称"> </el-table-column>
      <el-table-column prop="roleContent" label="角色描述"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="DeleteRole(scope.row.id)"
            >删除</el-button
          >
          <el-button
            size="mini"
            type="warning"
            @click="distributionMenu(scope.row.id)"
            >分配权限</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[3, 6, 9]"
      :page-size="from.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="totalCount"
    >
    </el-pagination>

    <el-dialog title="角色添加" :visible.sync="dialogFormVisible">
      <role-add ref="roleadd"></role-add>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="AddRole">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="角色修改" :visible.sync="dialogFormVisible1">
      <role-upd
        ref="roleupd"
        :Rid="roleId"
        v-if="dialogFormVisible1"
      ></role-upd>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible1 = false">取 消</el-button>
        <el-button type="primary" @click="UpdRole">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="分配权限" :visible.sync="dialogFormVisibledistribution">
      <role-menu-add
        ref="roleMenu"
        :Rid="roleId"
        v-if="dialogFormVisibledistribution"
      ></role-menu-add>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibledistribution = false"
          >取 消</el-button
        >
        <el-button type="primary" @click="distribution">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getRoles, deleteRole } from "@/api/role";
import RoleAdd from "./RoleAdd.vue";
import RoleUpd from "./RoleUpd.vue";
import RoleMenuAdd from "./RoleMenuAdd.vue";
export default {
  components: { RoleAdd, RoleUpd, RoleMenuAdd },
  data() {
    return {
      RoleData: [],
      from: {
        pageIndex: 1,
        pageSize: 3,
        RoleName: "",
      },
      totalCount: 0,
      dialogFormVisible: false,
      dialogFormVisible1: false,
      roleid: 0,
      dialogFormVisibledistribution: false,
      roleId: 0,
    };
  },
  methods: {
    //分配权限
    distributionMenu(Id) {
      this.dialogFormVisibledistribution = true;
      this.roleId = Id;
      console.log(this.roleId);
    },
    distribution() {
      this.$refs.roleMenu.AddRoleMenu();
    },
    getrolelist() {
      getRoles(this.from).then((res) => {
        this.RoleData = res.data;
        this.totalCount = res.totalCount;
      });
    },
    //全选
    handleSelectionChange() {},
    //分页
    handleSizeChange(val) {
      this.from.pageSize = val;
      this.getrolelist();
    },
    //分页
    handleCurrentChange(val) {
      this.from.pageIndex = val;
      this.getrolelist();
    },
    //添加
    AddRole() {
      this.$refs.roleadd.submitForm("submitForm");
    },
    //修改
    UpdRole() {
      this.$refs.roleupd.submitForm("submitForm");
      this.getrolelist();
    },
    //修改反填
    handleEdit(row) {
      this.dialogFormVisible1 = true;
      this.roleId = row.id;
    },

    //删除
    DeleteRole(id) {
      this.$confirm("此操作将永久删除该角色是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        deleteRole(id).then((res) => {
          if (res.code == 200) {
            this.$message.success("删除成功");
            this.getrolelist();
          } else {
            this.$message.error("删除失败");
          }
        });
      });
    },
  },
  created() {
    this.getrolelist();
  },
};
</script>
